/**
 * 
 */
package com.autonavi.car.si.FileStore.Config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.autonavi.car.si.FileStore.model.FileVO;
import com.autonavi.car.si.FileStore.util.ConfigUtil;
import com.autonavi.car.si.FileStore.util.DateUtil;
import com.autonavi.car.si.FileStore.util.StringUtil;

/**
 * FTP主机管理
 * 
 * @author kucheng.liao@autonavi.com
 * @date Nov 24, 2014
 * 
 */
public class FTPHostManager {
	/**
	 * 通过文件名，获取FTP节点
	 * 
	 * @param vo
	 * @return
	 */
	public List<FTPHost> getHostByFileName(FileVO vo) {
		int i = Math.abs(vo.getUniqueFileName().hashCode() % FileStoreConfig.getInstance().getStoreSliceNumber());
		vo.setSlice(i);
		vo.setDate(DateUtil.getCurrentDate());
		List<FTPHost> result = new ArrayList<FTPHost>();
		StringBuilder sb = new StringBuilder();
		sb.append(vo.getDate()).append(File.separator).append(i);
		vo.setStorePath(sb.toString());
		for (FTPHost host : storeNodes) {
			if (host.max < 0 || (host.min <= i && host.getMax() >= i)) {
				result.add(host);
			}
		}
		/**
		 * 默认无节点存储文件时，使用第一个存储节点
		 */
		if (result.size() == 0) {
			result.add(storeNodes.get(0));
		}
		return result;
	}

	/**
	 * 通过分片获取FTP节点
	 * 
	 * @param slice
	 * @return
	 */
	public List<FTPHost> getHostBySlice(int slice) {
		List<FTPHost> result = new ArrayList<FTPHost>();
		for (FTPHost host : storeNodes) {
			if (host.max < 0 || (host.min <= slice && host.getMax() >= slice)) {
				result.add(host);
			}
		}
		if (result.size() == 0) {
			result.add(storeNodes.get(0));
		}
		return result;
	}

	private static List<FTPHost> storeNodes = new ArrayList<FTPHost>();

	private static volatile boolean inited = true;

	public void init() {
		if (inited) {
			synchronized (FTPHostManager.class) {
				if (inited) {
					String nodestr = ConfigUtil.get("NODES");
					String[] nodes = nodestr.split(";");
					for (String node : nodes) {
						FTPHost host = new FTPHost();
						host.setName(node);
						host.setPort(Integer.parseInt(ConfigUtil.get(node + ".port")));
						host.setPwd(ConfigUtil.get(node + ".password"));
						host.setHostName(ConfigUtil.get(node + ".host"));
						String path = ConfigUtil.get(node + ".rootPath").trim();
						if (!path.endsWith(File.separator)) {
							path += File.separator;
						}
						host.setRootPath(ConfigUtil.get(node + ".rootPath"));
						host.setUser(ConfigUtil.get(node + ".user"));

						String max = ConfigUtil.get(node + ".max");
						if (StringUtil.isNotEmpty(max)) {
							host.setMax(Integer.parseInt(max));
						}
						String min = ConfigUtil.get(node + ".min");
						if (StringUtil.isNotEmpty(min)) {
							host.setMin(Integer.parseInt(min));
						}
						storeNodes.add(host);
					}

					inited = false;
				}
			}
		}
	}

	/**
	 * FTP主机
	 * 
	 * @author kucheng.liao@autonavi.com
	 * @date Nov 24, 2014
	 * 
	 */
	public static class FTPHost {

		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append(" ( name =").append(name).append(", hostName=").append(hostName).append(", port=").append(port)
					.append(", username=").append(user)/*.append(", password=").append(pwd)*/.append(", timeout=")
					.append(timeout).append(", min=").append(min).append(", max=").append(max);

			return sb.toString();
		}

		/**
		 * 名称，唯一标志位
		 */
		private String name;
		/**
		 * 主机名/ip
		 */
		private String hostName;
		/**
		 * 端口
		 */
		private int port;
		/**
		 * 用户
		 */
		private String user;
		/**
		 * 密码
		 */
		private String pwd;
		/**
		 * 超时时间
		 */
		private int timeout = 60000;

		private String rootPath = "/";

		private int min = -1;

		private int max = -1;

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @return the hostName
		 */
		public String getHostName() {
			return hostName;
		}

		/**
		 * @param hostName
		 *            the hostName to set
		 */
		public void setHostName(String hostName) {
			this.hostName = hostName;
		}

		/**
		 * @return the port
		 */
		public int getPort() {
			return port;
		}

		/**
		 * @param port
		 *            the port to set
		 */
		public void setPort(int port) {
			this.port = port;
		}

		/**
		 * @return the user
		 */
		public String getUser() {
			return user;
		}

		/**
		 * @param user
		 *            the user to set
		 */
		public void setUser(String user) {
			this.user = user;
		}

		/**
		 * @return the pwd
		 */
		public String getPwd() {
			return pwd;
		}

		/**
		 * @param pwd
		 *            the pwd to set
		 */
		public void setPwd(String pwd) {
			this.pwd = pwd;
		}

		/**
		 * @return the timeout
		 */
		public int getTimeout() {
			return timeout;
		}

		/**
		 * @param timeout
		 *            the timeout to set
		 */
		public void setTimeout(int timeout) {
			this.timeout = timeout;
		}

		/**
		 * @return the min
		 */
		public int getMin() {
			return min;
		}

		/**
		 * @param min
		 *            the min to set
		 */
		public void setMin(int min) {
			this.min = min;
		}

		/**
		 * @return the max
		 */
		public int getMax() {
			return max;
		}

		/**
		 * @param max
		 *            the max to set
		 */
		public void setMax(int max) {
			this.max = max;
		}

		/**
		 * @return the rootPath
		 */
		public String getRootPath() {
			return rootPath;
		}

		/**
		 * @param rootPath
		 *            the rootPath to set
		 */
		public void setRootPath(String rootPath) {
			this.rootPath = rootPath;
		}

	}

}
